home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6526 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: memory analysis tools where can I get them?
  5. Date: Mon, 19 Feb 1996 15:20:39 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31287927.25B0@cmt.lpr.mail.carel.fi>
  8. References: <rjv-1502961333560001@cdt.com>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Ralph J. Verrilli wrote:
  16. > does anyone know where I might be able to find tools to assist with memory
  17. > management.  I am specifically looking for tools that might help with
  18. > finding memory leaks and memory overwrites.
  19. > I'd like these tools be be relatively low cost since I need them to help
  20. > me finish my PhD.  Thanks.
  21. > Thanks.
  22. > Please respond to:  rjv@cdt.com
  23. > rj
  24.  
  25. One very low-cost solution would be to write a homegrown management system. It would go 
  26. something like this:
  27.  
  28.     #define    malloc(x)    MyMalloc(x, __FILE__, __LINE__)
  29.     #define    calloc(x, y)    MyCalloc(x, y, __FILE__, __LINE__)
  30.     #define    free(x)        MyFree(x, __FILE__, __LINE__)
  31.  
  32. Now, you'd use these in all but one of your source modules, so that when you 
  33. malloc/calloc/free memory, the functions being actually called are those of your own. 
  34. Then, in one module (let's name it mymem.c), you will _not_ use these definitions but 
  35. actually use the stdlib functions. Also, when allocating, you'll allocate four bytes more 
  36. at the beginning of the memory and four after it, like this:
  37.  
  38.     void    *MyMalloc(size_t size, char *file, long line)
  39.     {
  40.         char    *ptr = malloc(size + 8);
  41.  
  42.         memset(ptr, '\xfd', 4);
  43.         memset(ptr + size + 4, '\xfd', 4);
  44.         return ptr + 4;
  45.     }
  46.  
  47. You can also use a global list of your own to keep track of the allocations, like:
  48.  
  49.     struct {
  50.         char    *file;
  51.         long    line;
  52.         size_t    size;
  53.         void    *ptr;    /* data allocated */
  54.     } Allocd;
  55.  
  56. Now, when allocating, you can add an entry of this and set it up so when someone calls 
  57. free, you can check whether the memory was overwritten:
  58.  
  59.     void    MyFree(void *ptr, char *file, long line)
  60.     {
  61.         char    *p = (char *)ptr - 4;
  62.         int    i;
  63.  
  64.         for (i = 0; i < NumAllocd; i++) {
  65.             if ((ptr - 4) == Allocd[i].ptr) {
  66.                 /* Found this allocation */
  67.                 if (first_four_bytes != '\xfd' ||
  68.                     last_four_bytes != '\xfd)
  69.                     ; /* ERROR: MEMORY OVERWRITTEN ! */
  70.                 free(ptr);
  71.                 return;
  72.             }
  73.         }
  74.         /* ERROR: No such memory allocation done! */
  75.     }
  76.  
  77. Etc. If you track the allocations using an array, you can add a function to call 
  78. regularly that will step through the allocations and check if there has been memory 
  79. overwrites. Also, you can check at desired points in your program, if the allocated 
  80. memory has been freed that should have. When you have a memory overwrite, you can then 
  81. (assuming you use a debugger cabable of it) turn a memory breakpoint on in the 
  82. debugger and watch, where in your program some part overwrites the memory.
  83.  
  84. This is just a hint towards the direction, but you'll get the idea.
  85.  
  86. Happy memory hunting!
  87.  AriL
  88. -- 
  89. All my opinions are mine and mine alone.
  90.